<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing csv files.
 * @package backend-roles/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Athlete_bucket extends Backend_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->helper('cms');
        
        $this->load->model('athlete_buckets_model');
        $this->load->model('backend-screening/videos_model');
        $this->load->model('backend-screening-bucket/age_brackets_model');
        $this->load->model('backend-screening-bucket/sports_model');
        $this->load->model('backend-screening-bucket/positions_model');
        $this->load->model('ab_categories_model');

        $this->load->library('csvreader');
        $this->load->library('form_validation');
//        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small class="help-block">', '</small></div>');
        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small>', '</small>');

    }

    /**
     * Default method called when nothing is specified.
     * Displays dashboard.
     * @access public
     */
    public function index()
    {

        set_time_limit(0);

        $sport_id          = NULL;
        $age_bracket_id    = NULL;
        $position_id       = NULL;
        $category_id       = NULL;
        
        $this->csvreader->separator = '|';
        $screenings = $this->csvreader->parse_file('csvs/Athlete Buckets.csv');

        foreach($screenings as $screening)
        {
            // sports
            $sport_text = trim( $screening['sport'] );
            if( $sport_text )
            {
                $sport                              = array();
                $sport['Name']                      = $sport_text;
                $sport['DateCreated']               = date('Y-n-d H:i:s');
                $sport_id = $this->sports_model->push( array('Name' => $sport_text), $sport);
            }
            
            // positions
            $position_text = trim( $screening['position'] );
            if( $position_text )
            {
                $position                              = array();
                $position['Name']                      = $position_text;
                $position['DateCreated']               = date('Y-n-d H:i:s');
                $position_id = $this->positions_model->push( array('Name' => $position_text), $position);
            }
            
            // category
            $category_text                             = trim( $screening['category'] );
            if( $category_text )
            {
                $category                              = array();
                $category['Name']                      = $category_text;
                $category['DateCreated']               = date('Y-n-d H:i:s');
                $category_id = $this->ab_categories_model->push( array('Name' => $category_text), $category);
            }
            
            // age_brackets
            $age_bracket_text = trim( $screening['age_bracket'] );
            if( $age_bracket_text )
            {
                $age_bracket                              = array();
                $age_bracket['Name']                      = $age_bracket_text;
                $minmax                                   = explode('-', $age_bracket_text);
                $age_bracket['Minimum']                   = trim( $minmax[0] );
                $age_bracket['Maximum']                   = trim( $minmax[1] );
                $age_bracket['DateCreated']               = date('Y-n-d H:i:s');
                $age_bracket_id = $this->age_brackets_model->push( array('Name' => $age_bracket_text), $age_bracket);
            }

            // screening buckets
            $video_id = $this->videos_model->attribute( array( 'Code' => trim($screening['video']) ), 'VideoID')->get();
            if( $video_id && $sport_id && $age_bracket_id && $position_id && $category_id )
            {
                $athlete_buck                              = array();
                $athlete_buck['SportID']                   = $sport_id;
                $athlete_buck['PositionID']                = $position_id;
                $athlete_buck['AgeBracketID']              = $age_bracket_id;
                $athlete_buck['ABCategoryID']              = $category_id;
                $athlete_buck['VideoID']                   = $video_id;
                $athlete_buck['Week']                      = trim( $screening['week'] );
                
                $athlete_bucket                              = array();
                $athlete_bucket['SportID']                   = $sport_id;
                $athlete_bucket['PositionID']                = $position_id;
                $athlete_bucket['AgeBracketID']              = $age_bracket_id;
                $athlete_bucket['ABCategoryID']              = $category_id;
                $athlete_bucket['VideoID']                   = $video_id;
                $athlete_bucket['Week']                      = trim( $screening['week'] );
                $athlete_bucket['DateCreated']               = date('Y-n-d H:i:s');
                $athlete_bucket_id = $this->athlete_buckets_model->push( $athlete_buck, $athlete_bucket);
            }
            
        }

        echo '<pre>';
        print_r($screenings); 
        echo '</pre>';
        exit();
        
    }
}

/* End of file parsecsv.php */
/* Location: ./application/roles/backend-csvreader/controllers/parsecsv.php */